﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZLDC.Common.Estate;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;

namespace ZLDC.Topo.BLL.Estate
{
    public class lp_Addition_left
    {
        public static string GetDEPT(string deptid, DateTime txtTIME)
        {
            StringBuilder sb = new StringBuilder();
            string sql = "";
            sql += " select SUM(lm.Actual_Goal) as Actual_Goal, za.id as deptid,name,parent_id from ";
            sql += " t_zlhq_t_s_alldept za left join lp_Configuration_main lm on za.Id =lm.deptID ";
            sql += " where DltFlg=0 and parent_id = (select parent_id from t_zlhq_t_s_alldept where Id=" + deptid + " and DltFlg=0 ) ";
            sql += " and DATEDIFF(dd, workday, '" + txtTIME + "') = 0  and (lm.Actual_Goal >0) ";
            sql += " group by za.Id,za.name,za.parent_id ";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);

            int j = 0;
            sb.Append("<table border='0' cellpadding=\"0\" cellspacing=\"0\">");
            sb.Append("<tr>");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {


                if (j > 0 && j % 3 == 0)
                {
                    sb.Append("</tr>");
                    sb.Append("<tr>");
                }
                j++;
                sb.Append("<td valign='top'>");
                sb.Append("<table style='width:350px;float:left;text-align:center;margin-left:6px;' bgcolor='#a8c7ce'  border='0' cellpadding=\"0\" cellspacing=\"1\"><tr><td colspan=\"3\"  bgcolor='d3eaef' class='STYLE6'>");
                sb.Append(ds.Tables[0].Rows[i]["name"].ToString());

                sb.Append("<tr>");
                sb.Append("<td style='width:200px;text-align:center;' colspan=\"3\">");
                sb.Append(GetPeoPleList(ds.Tables[0].Rows[i]["deptid"].ToString(), i, txtTIME));
                sb.Append("</td>");
                sb.Append("</tr>");

                sb.Append("</table>");
                sb.Append("</td>");
            }
            sb.Append("</tr>");
            sb.Append("</table>");
            if (sb.ToString() == "")
            {
                return "<a style='color:red'>无参考数据</a>";
            }
            return sb.ToString();
        }

        public static string GetPeoPleList(string deptid, int j, DateTime txtTIME)
        {
            string sql = "select Actual_Goal,Other2 as user_name from lp_Configuration_main  where (total>0 or Actual_Goal>0) and DATEDIFF(dd, workday, '" + txtTIME + "') = 0 and deptid=" + deptid + "  order by Actual_Goal desc";
            DataSet ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(sql);
            string yg = "<table style='text-align:center;margin-top:5px;margin-left:66px;' border='0' cellpadding=\"0\" cellspacing=\"1\">";
            yg += "<tr>";
            yg += "<td  bgcolor='d3eaef' class='STYLE10'>";
            yg += "姓名";
            yg += "</td>";
            yg += "<td  bgcolor='d3eaef' class='STYLE10'>";
            yg += "分数";
            yg += "</td>";
            yg += "</tr>";
            StringBuilder sdept = new StringBuilder();
            sdept.Append("<graph caption='系统得分饼图' xAxisName='饼图' yAxisName='系统得分饼图' decimalPrecision='0' formatNumberScale='0'>");
            int num = 0; int numcount = ds.Tables[0].Rows.Count; for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                yg += "<tr>";
                yg += "<td>";
                yg += ds.Tables[0].Rows[i]["user_name"].ToString();
                yg += "</td>";
                yg += "<td>";
                yg += ds.Tables[0].Rows[i]["Actual_Goal"].ToString();
                num += int.Parse(ds.Tables[0].Rows[i]["Actual_Goal"].ToString() == "" ? "0" : ds.Tables[0].Rows[i]["Actual_Goal"].ToString());
                yg += "</td>";
                yg += "</tr>";


            }
            if (numcount > 0)
            {
                num = num / numcount;
                yg += "<tr>";
                yg += "<td>";
                yg += "平均分：";
                yg += "</td>";
                yg += "<td>";
                yg += num;
                yg += "</td>";
                yg += "</tr>";
            }

            ds = ZLDC.Topo.BLL.Estate.lp_Basis.Getds(ZLDC.Topo.BLL.Estate.lp_Report_list.GetSql(" where Other2=" + deptid, "'" + txtTIME.ToString("yyyy-MM-dd") + "'", "'" + txtTIME.ToString("yyyy-MM-dd") + "'"));
            if (ds.Tables.Count > 0)
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    sdept.Append("  <set name='" + ds.Tables[0].Rows[i]["BasisName"].ToString() + "' value='" + ds.Tables[0].Rows[i]["Actual_Goal"].ToString() + "' />");
                }

            sdept.Append("</graph>");
            yg += "</table>";
            StringBuilder bt = new StringBuilder();
            bt.Append(sdept.ToString());
            Literal lit = new Literal();
            //lit.Text = InfoSoftGlobal.FusionCharts.RenderChart("charts/Pie2D.swf", "", bt.ToString(), "a" + j + "", "330", "150", false, false);
            bt.Append(lit.Text);
            yg += bt.ToString();
            return yg;
        }
    }
}
